<?php


/**
 * Description of ThemeInterface:
 * Reads and Writes theme/template name to the template management xml file
 * using the Appconfiguration class to get xml management file path and filename
 *
 * @author Malekgene Rantho
 * Date: 22 May 2011
 * 
 */

//Include AppConfiguration definition and implementation
include ('AppConfiguration.php');

class ThemeInterface {
    
    //Create theme name variable
    private $theme;
    
    //Contruct Theme Interface
    function __construct() {
        
        //Create a handle/reference to application configuration class
        $AppConf = new AppConfiguration();
        
        //Include the pear class to manage xml files
        require_once("Config.php");
        
          //Create a reference to xml file manipulatoe
        $template = new Config;
        
        //Get full path of the xml file for storage of theme nae
        $baseDir = $AppConf->getThemePath().$AppConf->getThemeFileName();
        echo $baseDir;
        //Create reference to the xml file and parse it to php format
        $root =& $template->parseConfig($baseDir, 'XML');
        
        //If error occurs while parsing xml file stop application and return
        //error message
        if (PEAR::isError($root)) {
        die('Error while reading configuration: ' . $root->getMessage());
        }
        
        //Create xml root reference
        $TemplateRoot =& $root->getItem("section","template");
          
        //Create reference to template name element
        $themename =& $TemplateRoot->getItem("directive","templateName");
        
        //Read template name element attribute value
        $this->theme = $themename -> getContent();
    }
    
    //Returm the theme name
    public  function getThemeName()
    {
        $theme_name = $this->theme;
        return $theme_name;
    }
    //Set theme name and persist it to the xml file
    public function setThemeName($theme)
    {
        //Create a handler to application configuration variable
        $AppConf = new AppConfiguration();
        //Create an object of the pear config class
        $template= new Config;
        //template name to the argument value
        $theme_name["templateName"] = $theme;
        //Get xml full path
        $baseDir = $AppConf->getThemePath().$AppConf->getThemeFileName();
        
        //Parse theme_name as a php array to xml format
        $template->parseconfig($theme_name, 'PHPArray');
        
        //Create a root that the theme name should be stored in to
        $options = array('name' => 'template');
        
        //Write the template name value to the xml file
        $template->writeConfig($basedir, 'xml', $options);
        
        //Set theme to the newly selected theme within the class
        $this->theme = $theme;
    }
}

?>
